package com.project.demo.test;

import com.project.demo.parameter.MasterPrivateKey;
import com.project.demo.parameter.PublicKey;
import com.project.demo.parameter.SystemKey;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;

public class KeyParser {
    static Pairing pairing = PairingFactory.getPairing("params/curves/a.properties");


    public static SystemKey parseSystemKey(String keyString) {
        // 提取 publicKey 和 masterPrivateKey 部分
        int publicKeyStart = keyString.indexOf("publicKey=") + "publicKey=".length();
        int publicKeyEnd = keyString.indexOf(", masterPrivateKey=");
        String publicKeyStr = keyString.substring(publicKeyStart, publicKeyEnd);

        int masterPrivateKeyStart = keyString.indexOf("masterPrivateKey=") + "masterPrivateKey=".length();
        int masterPrivateKeyEnd = keyString.lastIndexOf(")");
        String masterPrivateKeyStr = keyString.substring(masterPrivateKeyStart, masterPrivateKeyEnd);

        // 解析 PublicKey
        PublicKey publicKey = parsePublicKey(publicKeyStr);

        // 解析 MasterPrivateKey
        MasterPrivateKey masterPrivateKey = parseMasterPrivateKey(masterPrivateKeyStr);
        SystemKey systemKey = new SystemKey();
        systemKey.setPublicKey(publicKey);
        systemKey.setMasterPrivateKey(masterPrivateKey);
        return systemKey;
    }

    private static PublicKey parsePublicKey(String publicKeyStr) {
        // 提取 h
        int hStart = publicKeyStr.indexOf("h=") + "h=".length();
        int hEnd = publicKeyStr.indexOf(" egg_a={");
        String hStr = publicKeyStr.substring(hStart, hEnd);
        Element h =  pairing.getG1().newElementFromBytes(hStr.getBytes());
        // 提取 egg_a 的 x 和 y
        int eggAXStart = publicKeyStr.indexOf("x=") + "x=".length();
        int eggAXEnd = publicKeyStr.indexOf(",y=");
        String egg_a_xStr = publicKeyStr.substring(eggAXStart, eggAXEnd);
        Element egg_a_x =  pairing.getG1().newElementFromBytes(egg_a_xStr.getBytes());


        int eggAYStart = publicKeyStr.indexOf("y=") + "y=".length();
        int eggAYEnd = publicKeyStr.indexOf("}, f=");
        String egg_a_yStr = publicKeyStr.substring(eggAYStart, eggAYEnd);
        Element egg_a_y =  pairing.getG1().newElementFromBytes(egg_a_yStr.getBytes());

        // 提取 f
        int fStart = publicKeyStr.indexOf("f=") + "f=".length();
        String fStr = publicKeyStr.substring(fStart);
        Element f =  pairing.getG1().newElementFromBytes(fStr.getBytes());

        PublicKey publicKey = new PublicKey();
        publicKey.setH(f);
        publicKey.setH(egg_a_y);
        publicKey.setH(egg_a_x);
        publicKey.setH(f);
        return publicKey;
    }

    private static MasterPrivateKey parseMasterPrivateKey(String masterPrivateKeyStr) {
/*        // 提取 beta
        int betaStart = masterPrivateKeyStr.indexOf("beta=") + "beta=".length();
        int betaEnd = masterPrivateKeyStr.indexOf(", g_alpha=");
        String beta = masterPrivateKeyStr.substring(betaStart, betaEnd);

        // 提取 g_alpha 的 x 和 y
        int gAlphaXStart = masterPrivateKeyStr.indexOf("g_alpha=") + "g_alpha=".length();
        int gAlphaXEnd = masterPrivateKeyStr.indexOf(",", gAlphaXStart);
        String g_alpha_x = masterPrivateKeyStr.substring(gAlphaXStart, gAlphaXEnd);

        int gAlphaYStart = gAlphaXEnd + 1;
        int gAlphaYEnd = masterPrivateKeyStr.indexOf(", alpha=");
        String g_alpha_y = masterPrivateKeyStr.substring(gAlphaYStart, gAlphaYEnd);

        // 提取 alpha
        int alphaStart = masterPrivateKeyStr.indexOf("alpha=") + "alpha=".length();
        String alpha = masterPrivateKeyStr.substring(alphaStart);

        return new MasterPrivateKey(beta, g_alpha_x, g_alpha_y, alpha);*/
        return null;
    }

    public static void main(String[] args) {
/*        String keyString = "SystemKey(publicKey=PublicKey(h=962325408160847322497527475537492791336490269936380889899101737958179348115999078992676550688452087193633776274816669707278467565614333963619371573838873,3761794999870585614935262545354343257269151595396705572097234502226184457511448902692821743341207647273205112745606613533234642304115129095183447081090452,0, egg_a={x=1561669377639635544270737053201228809230322717391477582745962823295537944380696422597614425834176699275572175069871174066096360146002381403817872271542113,y=5714225943737411999100194003978148894664360033342279980618438794705694493355949059248381231146237815899346274749384631358333635803722253074441748855657178}, f=7942994062540970877160594266111136255360590962421633287880774560186301231249576947982022209069602804237532477021573374719816897937047619736374874599764863,2138988682521998838654804557269636301408367212875398438131956878928713712536213865602864877474850040306834521652710637577011628745874439541670031112134315,0), masterPrivateKey=null)";
        keyString = "systemKey:SystemKey(publicKey=PublicKey(h=1902146529284369196267952296370882293872346486105110820850997525122291398884324307662997398383795148509874053696355796695926217976900598335871077715229583,5872038674149656713924170416663341089996277254264621122998891744221559601689302490829478848863542646779896457508779349118541460792151724019984944304139102,0, egg_a={x=3236563948120660013152419213446535054617338336534657105053201518788259103865107554819610699443849272826601088166434526616343877802613481846944725305975110,y=5652063279169494786387192069951395117613942625565380017018756334130347811807767582239122987011746183281491747126017119631946047186139807250557426018731971}, f=4552092652344116004488699694336501588842466022064552172692869014326365116138985955379468866246425242827803533951168170888881387162420826090143214123375677,7152361743501843232556066253388035340897011069693230466338449409348309478328952700171480715150498740914704674524334150065589956007962388599083168517481791,0), masterPrivateKey=null)";
        SystemKey systemKey = parseSystemKey(keyString);
        System.out.println(systemKey);*/


    }
}